// src/stores/auth.js

import { defineStore } from 'pinia';

export const useAuthStore = defineStore('auth', {
    state: () => ({
        token: localStorage.getItem('authToken') || null, // 初始化时从 localStorage 加载 token
    }),
    actions: {
        login(token) {
            this.token = token;
            localStorage.setItem('authToken', token); // 保存 token 到 localStorage
        },
        logout() {
            this.token = null;
            localStorage.removeItem('authToken'); // 从 localStorage 移除 token
        },
    },
    getters: {
        isAuthenticated: (state) => !!state.token, // 计算属性，检查是否已登录
    },
});
